<?php


namespace App\Http\Controllers\Order;


use App\Http\Controllers\Controller;
use App\Models\Order\OrderBecause;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Validator;
/**
 * @group 订单失败原因查看
 *
 * 采用 restful api
 *
 * Body Parameters 表示请求的所需要的参数
 *
 * Query Parameters 表示返回参数的名称
 */
class OrderBecauseController extends Controller
{
    /**
     * 订单失败原因查看
     *
     * @queryParam  name 订单失败原因
     *
     */
    public function index(){
        $query = OrderBecause::get();
        return response()->json($query,200);
    }
    /**
     * 订单失败原因查看单条
     *
     * @queryParam  name 订单失败原因
     *
     */
    public function show($pk){
        $data =OrderBecause::find($pk);
        if($data == []){
            return response()->json(["message"=>"没有该信息"],406);
        }else{
            return response()->json($data,200);
        }
    }
    /**   订单失败原因添加
     *
     * @bodyParam name required  订单失败原因
     *
     * 504 添加失败
     */
    public function store(Request $request)
    {

        try{
            $e = genValidate();
            $validator = Validator::make($request->all(),$e);
            if($validator->fails()){
                return response()->json($validator->errors()->messages(),400);
            }
            $data=$request->all();
            OrderBecause::insertGetId($data);

            return response()->json(["message"=>"添加成功"],200);
        }catch (QueryException $e){
            return response()->json(["message"=>"添加失败"],504);
        }
    }
    /**
     * 订单失败原因修改
     *
     * @bodyParam name required  订单失败原因
     *
     * 503 修改失败
     */
    public function update(Request $request, $pk){
        $e = genValidate();
        $data=$request->all();
        $validator = Validator::make($request->all(),$e);
        if($validator->fails()){
            return response()->json($validator->errors()->messages(),400);
        }
        $carkind = OrderBecause::find($pk);
        if($carkind == []){
            return response()->json(["message"=>"没有该信息"],404);
        }
        try {
            OrderBecause::where('id',$pk)->update(['name'=>$request->get('name')]);
            return response()->json(["message"=>"修改成功"],200);
        }catch (QueryException $e){
            return response()->json(["message"=>"修改失败"],503);
        }
    }
    /**
     * 订单失败原因删除
     *
     */
    public function destroy(Request $request,$pk){
        $data = OrderBecause::find($pk);
        if($data == []){
            return response()->json(["message"=>"没有该信息"],404);
        }
        $data->delete();
        return response()->json(["message"=>"删除成功"],200);
    }
}